package com.mingren.manager.websocket;

import com.mingren.manager.websocket.PictureEditHandler;
import com.mingren.manager.websocket.WsHandshakeInterceptor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;

import javax.annotation.Resource;

/**
 * @author 曹宏福
 * @version 1.0
 * 编写WebSocket 指定路径配置器处理器和拦截器
 */
@Configuration
@Slf4j
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {


    @Resource
    private PictureEditHandler pictureEditHandler;

    @Resource
    private WsHandshakeInterceptor wsHandshakeInterceptor;

    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry registry) {
        //websocket
        registry.addHandler(pictureEditHandler, "/ws/picture/edit").addInterceptors(wsHandshakeInterceptor).setAllowedOrigins("*");
        //pictureEditHandler 是一个实现了 WebSocketHandler 接口的对象，它负责处理客户端与服务器之间的 WebSocket 通信。
        // "/ws/picture/edit" 是 WebSocket 端点的路径，客户端需要使用这个路径来建立 WebSocket 连接
        // .addInterceptors 是一个链式调用的方法，用于为 WebSocket 连接添加握手拦截器
        // 允许所有跨源请求
    }
}
